//
//  BlackUserListViewController.m
//  gamebox_qpp
//
//  Created by 赵隆杰 on 2022/2/12.
//

#import "BlackUserListViewController.h"

@interface BlackUserListViewController ()

@property (nonatomic, strong) NSMutableArray *dataSources;

@end

@implementation BlackUserListViewController

- (NSMutableArray *)dataSources
{
    if (!_dataSources) {
        _dataSources = [NSMutableArray array];
    }
    return _dataSources;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.centerTitle.text = @"黑名单用户";
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.mas_equalTo(UIDevice.vg_navigationFullHeight);
    }];
    
    self.tableView.estimatedRowHeight = 50;
    self.tableView.rowHeight = 50;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    [self refreshData];
}

- (void)refreshData
{
    NSArray *array = [LocalData getShieldUsers];
    self.dataSources = [UserInfoModel mj_objectArrayWithKeyValuesArray:array];
    [self.tableView reloadData];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSources.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UserInfoModel *user = self.dataSources[indexPath.row];
    NSString *cellText = @"奇葩用户";
    if (user.nickname.length) {
        cellText = user.nickname;
    } else {
        if (user.account.length) {
            cellText = user.account;
        }
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    cell.textLabel.text = cellText;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
      // 删除
      return UITableViewCellEditingStyleDelete;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    UserInfoModel *user = self.dataSources[indexPath.row];
    //只要实现这个方法，就实现了默认滑动删除！！！！！
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // 删除数据
        [LocalData deleteShieldUser:user.userId];
        [self.dataSources removeObjectAtIndex:indexPath.row];
        [tableView deleteRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
      return @"删除";
}

@end
